﻿#include "systemmainwid.h"
#include "ui_systemmainwid.h"
#include <QFileDialog>
#include <qmessagebox.h>
#include "componentmanager.h"


SystemMainWid::SystemMainWid(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::SystemMainWid())
{
	ui->setupUi(this); 

	auto plateM = component->GetComponent(Component::component_unstackPlate);
	if (plateM == nullptr) {
		ui->comboBox->addItem("未加载到任何托盘");
	}
	QByteArray result;
	plateM->ExcuteCmd(1, {}, result);
	QStringList name = QString(result).split("-");
	ui->comboBox->addItems(name);
	for (auto& iter : name) {
		auto temp = static_cast<UnstackPlate*>(plateM->GetPoint(iter));
		m_plates.insert(iter, temp);
	}

}

SystemMainWid::~SystemMainWid()
{
	delete ui;
}

// rgb
void SystemMainWid::on_toolButton_clicked()
{
	QString savePath = QFileDialog::getOpenFileName(
		this,
		"保存文件",
		QDir::homePath(),
		"所有文件 (*.*)"
	);

	if (!savePath.isEmpty()) {
		// 可选：加载图片到 QLabel
		QPixmap pixmap(savePath);
		if (!pixmap.isNull()) {
			ui->label_3->setPixmap(pixmap.scaled(ui->label_3->size(), Qt::KeepAspectRatio));
			ui->lineEdit->setText(savePath);
			return;
		}
	}
	QMessageBox::warning(this, "WARNING", "图片不存在");
}
//depth
void SystemMainWid::on_toolButton_2_clicked()
{
	QString savePath = QFileDialog::getOpenFileName(
		this,
		"保存文件",
		QDir::homePath(),
		"所有文件 (*.*)"
	);

	if (!savePath.isEmpty()) {
		// 可选：加载图片到 QLabel
		QPixmap pixmap(savePath);
		if (!pixmap.isNull()) {
			ui->label_4->setPixmap(pixmap.scaled(ui->label_4->size(), Qt::KeepAspectRatio));
			ui->lineEdit_2->setText(savePath);
			return;
		}
	}
	QMessageBox::warning(this, "WARNING", "图片不存在");
}
// snap
void SystemMainWid::on_pushButton_clicked()
{
	QString rgb, depth;
	if (m_current) {
		if (m_current->SnapPicture(rgb, depth)) {
			QPixmap pixmap(rgb);
			if (!pixmap.isNull()) {
				ui->label_3->setPixmap(pixmap.scaled(ui->label_3->size(), Qt::KeepAspectRatio));
				ui->lineEdit->setText(rgb);
			}
			QPixmap pixmap1(depth);
			if (!pixmap1.isNull()) {
				ui->label_4->setPixmap(pixmap1.scaled(ui->label_4->size(), Qt::KeepAspectRatio));
				ui->lineEdit_2->setText(depth);
				return;
			}
		}
	} else {
		QMessageBox::warning(this, "WARNING", "未选择托盘");
	}
}

// plate
void SystemMainWid::on_pushButton_2_clicked()
{
	QString name = ui->comboBox->currentText();
	m_current = m_plates[name];
	if (m_current == nullptr) {
		QMessageBox::warning(this, "WARNING", "获取托盘对象失败");
	}
}

// vis
void SystemMainWid::on_pushButton_3_clicked()
{
	QString rgb = ui->lineEdit->text();
	if (rgb.isEmpty() || m_current == nullptr) {
		QMessageBox::warning(this, "WARNING", "托盘对象或者彩色图路径为空");
		return;
	}
	QString vis;
	QByteArray ret;
	if (m_current->RgbRcognize(rgb, vis, ret)) {
		QPixmap pixmap(vis);
		if (!pixmap.isNull()) {
			ui->label_7->setPixmap(pixmap.scaled(ui->label_7->size(), Qt::KeepAspectRatio));
		}
	}

	ui->lineEdit_3->setText(ret);
}
void SystemMainWid::on_pushButton_4_clicked()
{
	QString savePath = QFileDialog::getOpenFileName(
		this,
		"选择文件",
		QDir::homePath(),
		"所有文件 (*.*)"
	);

	if (!savePath.isEmpty()) {
		QFile file(savePath);
		if (!file.open(QIODevice::ReadOnly)) {
			QMessageBox::warning(this, "WARNING", "文件错误");	
			return;
		}
		// 使用 QTextStream 读取内容（自动处理编码）
		QTextStream in(&file);
		QString content = in.readAll();  // 读取全部内容
		ui->lineEdit_3->setText(content);
		// 关闭文件
		file.close();
	}
	QMessageBox::warning(this, "WARNING", "文件不存在");
}

// cal
void SystemMainWid::on_pushButton_5_clicked()
{
	if (m_current == nullptr) {
		QMessageBox::warning(this, "WARNING", "托盘对象为空");
		return;
	}
	TestAlgParam param = {};
	param.depthPath = ui->lineEdit_2->text();
	param.jig = ui->spinBox->value();
	param.taskNum = ui->spinBox_2->value();
	param.rgbPath = ui->lineEdit->text();
	param.vis = ui->lineEdit_3->text().toUtf8();
	param.sku.height = ui->spinBox_5->value();
	param.sku.length = ui->spinBox_3->value();
	param.sku.width = ui->spinBox_4->value();
	QString result;
	QByteArray img;
	m_current->Caculate(param, result, img);
	ui->textBrowser->setText(result);
	QPixmap image;
	if (image.loadFromData(img, "png")) {
		ui->label_8->setPixmap(image.scaled(ui->label_8->size(), Qt::KeepAspectRatio));
	} else {
		ui->label_8->setText("图片为空");
	}
}
